<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}

// 获取表模型
$table_er_kami_type = C::t('#erling_kami_apibuy#er_kami_apibuy_type');

if($_GET['action_kami_apibuy']){//处理增删改
    if($_GET['formhash'] != FORMHASH){
        exit('error');
    }
    if($_GET['action_kami_apibuy'] == 'delete'){
        $table_er_kami = C::t('#erling_kami_apibuy#er_kami_apibuy');
        try {
            DB::query('START TRANSACTION');
            $table_er_kami_type->delete($_GET['action_kami_apibuy_id']);
            $table_er_kami->delete_by_kami_type($_GET['action_kami_apibuy_id']);
            DB::query('COMMIT');
        } catch (Exception $e) {
            DB::query('ROLLBACK');
            cpmsg('操作失败: '.$e->getMessage(), '', 'error');
            exit;
        }
    }elseif($_GET['action_kami_apibuy'] == 'edit'){
        $data = array(
            'name' => $_POST['name'],
            'group_id' => intval($_POST['group_id']),
            'extcredits' => intval($_POST['extcredits']),
            'credit' => intval($_POST['credit']),
            'status' => intval($_POST['status']),
            'sort' => intval($_POST['sort']),
            'desc' => $_POST['desc'],
            'is_api' => intval($_POST['is_api']),
            'configapi_id' => intval($_POST['configapi_id']),
            'api_type_id' => intval($_POST['api_type_id']),
            'update_time' => TIMESTAMP
        );
        $table_er_kami_type->update($_GET['action_kami_apibuy_id'], $data);
    }elseif($_GET['action_kami_apibuy'] == 'add'){
        $data = array(
            'name' => $_POST['name'],
            'group_id' => intval($_POST['group_id']),
            'extcredits' => intval($_POST['extcredits']),
            'credit' => intval($_POST['credit']),
            'status' => intval($_POST['status']),
            'sort' => intval($_POST['sort']),
            'desc' => $_POST['desc'],
            'is_api' => intval($_POST['is_api']),
            'configapi_id' => intval($_POST['configapi_id']),
            'api_type_id' => intval($_POST['api_type_id']),
            'create_time' => TIMESTAMP,
            'update_time' => TIMESTAMP
        );
        $table_er_kami_type->insert($data);
    }
    // 刷新
    $url_page = ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_kami_apibuy&pmod=admin_kami_apibuy_type';
    header('Location: '.$url_page);
    exit;
}

$url_page = ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_kami_apibuy&pmod=admin_kami_apibuy_type';

// 获取当前列表
$page = max(1, intval($page));
$pagesize = max($_GET['pagesize']??20, intval($pagesize));
$start = ($page - 1) * $pagesize;

$query = [];
if(!empty($_GET['id'])){
    $query['id'] = intval($_GET['id']);
    $url_page .= '&id='.$_GET['id'];
}
if(!empty($_GET['group_id'])){
    $query['group_id'] = intval($_GET['group_id']);
    $url_page.= '&group_id='.$_GET['group_id'];
}
if(!empty($_GET['name'])){
    $query['name'] = $_GET['name'];
    $url_page .= '&name='.$_GET['name'];
}
if(!empty($_GET['extcredits'])){
    $query['extcredits'] = intval($_GET['extcredits']);
    $url_page .= '&extcredits='.$_GET['extcredits'];
}
if(!empty($_GET['credit'])){
    $query['credit'] = intval($_GET['credit']);
    $url_page .= '&credit='.$_GET['credit'];
}
$result = $table_er_kami_type->fetch_all($page, $pagesize, $query);

$kami_list = $result['data'];
$total = $result['total'];

if(!empty($_GET['pagesize'])){
    $url_page .= '&pagesize='.intval($_GET['pagesize']);
}
$multipage = multi($total, $pagesize, $page, $url_page);

// 积分类型
$extcredits_array = DB::fetch_all("SELECT * FROM %t WHERE skey LIKE %s", array('common_setting', 'extcredits%'));
if ($extcredits_array && isset($extcredits_array[0]['svalue'])) {
    $extcredits_data = unserialize($extcredits_array[0]['svalue']);
} else {
    $extcredits_data = [];
}

// 获取分类的分组
$table_er_kami_typegroup = C::t('#erling_kami_apibuy#er_kami_apibuy_typegroup');
$group_list = $table_er_kami_typegroup->fetch_all();
$group_list = $group_list['data'];

// 其他
$table_er_kami_apibuy_configapi = C::t('#erling_kami_apibuy#er_kami_apibuy_configapi');
$configapi_list = $table_er_kami_apibuy_configapi->fetch_all();
$configapi_list = $configapi_list['data'];

include template('erling_kami_apibuy:admin_kami_apibuy_type');
